<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            给定两个整数 n 和 k，返回 1 ... n 中所有可能的 k 个数的组合。
            第一时间，如果k=2，那么很简单只需要双重for循环即可。k=3就三重for循环但如果k=100...那就g
            
            思路：组合问题用回溯，回溯就需要抽象成树形结构
            
            */
            var combine = function (n, k) {
                let path = []
                let res = []
                let startIndex = 1
                function backTracking(path, startIndex) {
                    if (path.length == k) {
                        return res.push([...path])
                    }
                    for (let i = startIndex; i <= n; i++) {
                        path.push(i)
                        backTracking(path, i + 1)
                        path.pop()
                    }
                }
                backTracking(path, startIndex)
                return res
            }
        </script>
    </body>
</html>
